/**
 * 基本信息标签页相关代码
 */
var lxr = (function() {

	var customerId = qmsk.getQueryValue("customerId");
	var $saveButton = $("<div class='BottomBtn'><input type='button' value='保存信息'></div>");

	var cache = {};

	function clearInput(id) {
		$("#" + id).find("input[type='text']").val('');
		$("#" + id).find("input[type='checkbox']").val('');
		$("#" + id).find("input[type='radio']").val('');
		$("#" + id).find("select").val('');
		$("#" + id).find("textarea").val('');
	}

	function disabledInput(id) {
		$("#" + id).find("input[type='text']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='checkbox']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='radio']").attr('disabled', 'disabled');
		$("#" + id).find("select").attr('disabled', 'disabled');
		$("#" + id).find("textarea").attr('disabled', 'disabled');
	}

	function removeDisabledInput(id) {
		$("#" + id).find("input[type='text']").removeAttr('disabled');
		$("#" + id).find("input[type='checkbox']").removeAttr('disabled');
		$("#" + id).find("input[type='radio']").removeAttr('disabled');
		$("#" + id).find("select").removeAttr('disabled');
		$("#" + id).find("textarea").removeAttr('disabled');
	}

	/*
	 * 页面加载的时候进行的初始化
	 */
	function loadInit() {
		clearInput("lxr_lxrxx");
		loadProvince();
		$("#lxr_add").unbind().bind('click', function() {
			clearInput("lxr_lxrxx");
			removeDisabledInput("lxr_lxrxx");
			$saveButton.remove();
			$("#lxr_lxrxx").append($saveButton);
			$saveButton.unbind().bind('click', insert);
		});
		loadSimpleList();
	}

	function insert() {
		var data = collectionData();
		qmsk.post('../friend/insert/' + customerId, data, function() {
			artSuccess('添加成功');
			$saveButton.remove();
			disabledInput("lxr_lxrxx");
			loadSimpleList();
		});
	}

	//加载省份
	function loadProvince() {
		var provinceId = "f_province";
		$("#" + provinceId).unbind('change');
		$("#" + provinceId).bind('change', function() {
			loadCity();
			$("#f_location").empty().attr('disabled', 'disabled');
		});
		qmsk.ansyInitSelect({
			id : provinceId,
			url : '../location/province',
			heads : ['code', 'name'],
			defaults : {
				key : '选择省'
			}
		});
	}

	/**
	 * 选择好省后加载市
	 */
	function loadCity() {
		var cityId = "f_city";
		$("#" + cityId).unbind('change');
		$("#" + cityId).bind('change', loadCountry);
		var code = $("#f_province").val();
		if (code !== "选择省" && qmsk.isNotEmpty(code)) {
			$("#" + cityId).removeAttr('disabled');
			qmsk.ansyInitSelect({
				id : cityId,
				url : '../location/city/' + code,
				heads : ['code', 'name'],
				defaults : {
					key : '选择市'
				}
			});
		} else {
			$("#" + cityId).empty().attr('disabled', 'disabled');
		}
	}

	/**
	 * 选择号市后 加载区县
	 */
	function loadCountry() {
		var countryId = "f_location";
		$("#" + countryId).removeAttr('disabled');
		var code = $("#f_city").val();
		if (qmsk.isNotEmpty(code) && code != '选择市') {
			qmsk.ansyInitSelect({
				id : countryId,
				url : '../location/countries/' + code,
				heads : ['code', 'name']
			});
		} else {
			$("#" + countryId).empty().attr('disabled', 'disabled');
		}
	}

	function collectionData() {
		var heads = ["name", "sex", "birthday", "idNumber", "nation", "relation", "tel", "mp", "location", "address", "zipcode"];
		var ids = [];
		for (var i = 0; i < heads.length; i++) {
			ids[i] = 'f_' + heads[i];
		}
		var data = qmsk.collectionData(heads, ids);
		return data;
	}

	function loadSimpleList() {

		qmsk.get('../friend/find/list/' + customerId + '/0/99', function(response) {
			cache.friendList = response;
			qmsk.dataTable({
				tbodyId : 'lxr_list',
				heads : ["name", "sex", "relation", "tel", "mp", "id"],
				response : response,
				sup : ['lxr_list_sup', '共 [index] 条'],
				handler : [[1, sexHandler], [2, relactoinHandler], [5, detailButton]]
			});
		});

		var relactoinHandler = function(relation, row) {
			if (relation == 0)
				return '亲属';
			if (relation == 1)
				return '朋友';
			return '';
		};
		var sexHandler = function(sex, row) {
			if (sex == 0)
				return '男';
			else if (sex == 1)
				return '女';
			else
				return '';
		};
		var detailButton = function(id, row) {
			return "<input onclick='_loadFriendDetail(" + row + ")' type='button' class='common_btn' value='查看详情'>";
		};

	}


	window._loadFriendDetail = loadDetail;

	function loadDetail(row) {
		$saveButton.remove();
		var response = cache.friendList;
		bindData(response, row);
		disabledInput("lxr_lxrxx");
		$("#lxr_edit").unbind().bind('click', function() {
			removeDisabledInput("lxr_lxrxx");
			$saveButton.remove();
			$("#lxr_lxrxx").append($saveButton);
			$saveButton.unbind().bind('click', update);
		});

	}

	function update() {
		var id = cache.id;
		var data = collectionData();
		qmsk.post('../friend/update/' + id, data, function() {
			artSuccess('更新成功!');
			$saveButton.remove();
			disabledInput("lxr_lxrxx");
			loadSimpleList();
		});
	}

	function bindData(response, row) {
		var ids = [];
		var heads = response.heads;
		var temp = response.getObject(heads, row);
		cache.id = temp.id;
		var data = {};
		for (var i = 0; i < heads.length; i++) {
			data['f_' + heads[i]] = temp[heads[i]];
		}
		qmsk.bindBean(data);

		var f_location = "" + temp.location;
		var f_province = f_location.substring(0, 2) + '0000';
		var f_city = f_location.substring(0, 4) + '00';
		$("#f_province").val(f_province);
		loadCity();
		$("#f_city").val(f_city);
		loadCountry();
		$("#f_location").val(f_location);

	}

	return {
		init : function() {
			loadInit();
		}
	};

})();
